package GcExcelTest_maven.GcExcelTest_maven;

import com.grapecity.documents.excel.Workbook;
import com.grapecity.documents.excel.template.DataSource.JsonDataSource;

import java.io.*;

public class Test {


    public static void main(String[] args) throws Exception {
        test("cross");
        test("matrix");
    }

    public static void test(String name) {
        Workbook wb = new Workbook();
        wb.open(name+".xlsx");
        wb.getNames().add("TemplateOptions.KeepLineSize", "true");
        JsonDataSource datasource = new JsonDataSource(getJsonStr(name+".json"));
        wb.addDataSource("ds", datasource);
        wb.processTemplate();
        wb.save(name + "_result.xlsx");
    }

    public static String getJsonStr(String datasource_name) {
        String jsonStr = "";
        try {
            File jsonFile = new File(datasource_name);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}